/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TextNor.Expand;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

/**
 *
 * @author Pham_Thanh
 */
public final class Expand {

    public HashMap expnHm = new HashMap();
    public Pef pef = new Pef();
//    public static void main(String argv[]) throws UnsupportedEncodingException, FileNotFoundException, IOException, XMLStreamException{
//        Expand ex=new Expand();
//    }

    public Expand(String inf, String ouf) throws UnsupportedEncodingException, FileNotFoundException, IOException, XMLStreamException {

        System.out.println("package TextNor.Expand; class Expand");

        readDataFile();
        writeToXMLFile(inf, ouf);
    }

    public void readDataFile() throws UnsupportedEncodingException, FileNotFoundException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("data\\expand\\expn_full.db"), "UTF8"));
        String line;
        while ((line = br.readLine()) != null) {
            StringTokenizer st = new StringTokenizer(line, "|\n\r\f");
            while (st.hasMoreTokens()) {
                String expn = st.nextToken();
                String full = st.nextToken();
                int count = Integer.parseInt(st.nextToken());

                if (expnHm.containsKey(expn)) {
                    ArrayList fullAr = (ArrayList) expnHm.get(expn);
                    fullAr.add(full);
                } else {
                    ArrayList fullAr = new ArrayList();
                    fullAr.add(full);
                    expnHm.put(expn, fullAr);
                }
            }
        }
    }

    public String guessFull(String expn) {
        String gFull = "";
        double gPfe = 0;
        ArrayList fullAr = (ArrayList) expnHm.get(expn);
        if (fullAr == null) {
            return gFull;
        }
        for (int i = 0; i < fullAr.size(); i++) {
            String full = (String) fullAr.get(i);
            double pfe = pef.calculate(expn, full);
            if (pfe > gPfe) {
                gFull = full;
                gPfe = pfe;
            }
        }

        return gFull;
    }

    public void writeToXMLFile(String inf, String ouf) throws FileNotFoundException, XMLStreamException, UnsupportedEncodingException, IOException {

        FileInputStream fis = new FileInputStream(inf);
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);

        FileOutputStream fos = new FileOutputStream(ouf);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");
        XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(osw);

        xmlStreamWriter.writeStartElement("root");

        int index = 0;
        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {

                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("record")) {
                        String RecId = xmlStreamReader.getAttributeValue(0);
                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeAttribute("RecId", RecId);

                    }

                    if (localName.equals("URL")) {
                        String url = xmlStreamReader.getElementText();

                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeCharacters(url);
                        xmlStreamWriter.writeEndElement();

                    }
                    if (localName.equals("Infomation")) {
                        xmlStreamWriter.writeStartElement(localName);


                    }

                    if (localName.equals("W")) {
                        String atVal = xmlStreamReader.getAttributeValue(0);
                        if (atVal.equals("EXPN")) {
                            String nsw = xmlStreamReader.getElementText();
                            String full = guessFull(nsw);
                            if (full.equals("")) {
                                xmlStreamWriter.writeStartElement(localName);
                                xmlStreamWriter.writeAttribute("NSW", "SEQN");
                                xmlStreamWriter.writeCharacters(nsw);
                                xmlStreamWriter.writeEndElement();
                            } else {
                                xmlStreamWriter.writeStartElement(localName);
                                xmlStreamWriter.writeAttribute("NSW", "EXPN");
                                xmlStreamWriter.writeAttribute("FULL", full);
                                xmlStreamWriter.writeCharacters(nsw);
                                xmlStreamWriter.writeEndElement();
                            }


                        } else {
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            String nsw = xmlStreamReader.getElementText();
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }


                    }

                    if (localName.equals("SPLIT")) {
                        xmlStreamWriter.writeStartElement(localName);
                    }


                    break;

                case XMLStreamConstants.END_ELEMENT:
                    String endLocalName = xmlStreamReader.getLocalName();
                    //System.out.println(endLocalName);
                    if (endLocalName.equals("record")) {
                        xmlStreamWriter.writeEndElement();

                    }
                    if (endLocalName.equals("Infomation")) {
                        xmlStreamWriter.writeEndElement();

                    }

                    if (endLocalName.equals("SPLIT")) {
                        xmlStreamWriter.writeEndElement();
                    }



                    break;
                case XMLStreamConstants.CHARACTERS:
                    String character = xmlStreamReader.getText();
                    xmlStreamWriter.writeCharacters(character);
                    break;

                default:
                    break;
            }
        }


        xmlStreamWriter.writeEndElement();

        xmlStreamReader.close();
        xmlStreamWriter.close();
        fis.close();
        fos.close();
    }
}
